package com.unicom.office.templatefunc;

import com.unicom.office.runtime.OfficeRuntime;
import com.unicom.office.type.TypeItem;
import com.unicom.office.dataitem.ListDataItem;
import com.unicom.office.dataitem.StringDataItem;
import com.unicom.office.node.OfficeNode;
import com.unicom.office.node.OfficeNodeType;
import com.unicom.office.unit.OfficeUnit;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SizeFunc extends TemplateFunc {
    public Set<OfficeNodeType> getNodeTypes() {
        HashSet<OfficeNodeType> set = new HashSet<OfficeNodeType>();
        set.add(OfficeNodeType.TextRange);
        return set;
    }

    public String getName() {
        return "size";
    }

    public TypeItem getType() {
        return null;
    }

    public Object runForRender(List<Object> params, OfficeNode currentNode) {
        Object param = params.get(0);
        if(param instanceof ListDataItem) {
            return ((ListDataItem)param).getValue().size();
        }else if(param instanceof StringDataItem){
            return ((StringDataItem)param).getValue().length();
        }else{
            throw new RuntimeException("不支持的数据类型");
        }
    }

    public Object run(List<Object> params, OfficeRuntime officeRuntime) {
        return null;
    }

    public void runForMatch(OfficeUnit unit, List<Object> params, OfficeRuntime officeRuntime) {

    }
}
